header myI2CHeader
{
   // Basic Script Showing How To Read and Log a Temperature from an:
   // AD7414 digital I2C sensor, by Mauro Grassi.
   // Define a Constant which is the sensor's I2C Address 
   #I2C_ADDRESS=0x92;
}

script myI2CScript
{
	// Open the I2C bus, running at 400kHz...
	@@openI2C(400);
	precision(3);
	print "The Bus Speed is: ", $$hardware.I2C.busRate, newline;
	while(1)
	{
		// Write the Address Register
		$RESULT=@@putI2CByte(#I2C_ADDRESS, 0);
		if($RESULT)
		{
			// Read Two Bytes From The Sensor (the address increments automatically)
			$RESULT=@@getI2C(#I2C_ADDRESS, 2);
			if($RESULT)
			{
				// Compute the Temperature
				$T=$$I2C(0)+($$I2C(1)/256.0);
				print "The Temperature is ", $T, " degrees Celsius.", newline;
			} 
		} 
		else
		{
			print "Read Error.", newline;
		}
		sleep(3);
	}
}
